<?php
/**
 * [物料版]初始化升迁脚本中空基础物料条码赋予基础物料编码
 * @author: wangbiao@shopex.cn
 */
error_reporting(E_ALL ^ E_NOTICE);

$domain = $argv[1];
$order_id = $argv[2];
$host_id = $argv[3];

if (empty($domain) || empty($order_id) || empty($host_id)) {
    die('No Params');
}

set_time_limit(0);
require_once(dirname(__FILE__) . '/../../lib/init.php');
cachemgr::init(false);

$db     = kernel::database();

#step 1
$total   = 0;
$succ    = 0;
$fail    = 0;

$page     = 0;#开始页
$limit    = 500;#每次执行数量

$result    = batch_basic_material_code();

echo("step 1: 批量更新基础物料条码已完成...\n\n");

/*
 * 批量更新基础物料条码为空的更新为基础物料编码
 *
 */
function batch_basic_material_code()
{
    global $db, $page, $limit, $fail;
    
    #查询
    $sql    = "SELECT a.bm_id, a.material_bn, b.code FROM sdb_material_basic_material AS a LEFT JOIN sdb_material_codebase AS b ON a.bm_id=b.bm_id 
               WHERE 1 LIMIT ". ($page * $limit) .", " . $limit;
    $dataList    = $db->select($sql);
    
    #没有可执行的货品,则退出.
    if(empty($dataList))
    {
        return false;
    }
    
    #加入事务
    //$db->beginTransaction();
    
    foreach ($dataList as $key => $val)
    {
        if(!empty($val['code']))
        {
            continue;
        }
        
        #删除对应物料条码记录
        $del_sql    = "DELETE FROM sdb_material_codebase WHERE bm_id=". $val['bm_id'] . " AND type=1";
        $db->exec($del_sql);
        
        #插入物料条码记录
        $insert_sql    = "INSERT INTO sdb_material_codebase SET bm_id=". $val['bm_id'] .", type=1, code='". $val['material_bn'] ."'";
        $db->exec($insert_sql);
    }
    
    #提交事务
    //$db->commit();
    
    #release
    unset($dataList, $sql, $del_sql, $insert_sql);
    
    $page++;
    usleep(500000);#暂停0.5秒
    
    #递归调用
    batch_basic_material_code();
}


#step 2
$total   = 0;
$succ    = 0;
$fail    = 0;

$page     = 0;#开始页
$limit    = 500;#每次执行数量

$result    = batch_basic_material_conf_special();

echo("step 2: 批量初始化特殊扫码配置已完成...\n\n");

/*
 * 批量初始化特殊扫码配置
 *
 */
function batch_basic_material_conf_special()
{
    global $db, $page, $limit, $fail;
    
    #查询
    $sql    = "SELECT a.bm_id, a.material_bn, b.openscan, b.fromposition FROM sdb_material_basic_material AS a 
               LEFT JOIN sdb_material_basic_material_conf_special AS b ON a.bm_id=b.bm_id 
               WHERE 1 LIMIT ". ($page * $limit) .", " . $limit;
    $dataList    = $db->select($sql);
    
    #没有可执行的货品,则退出.
    if(empty($dataList))
    {
        return false;
    }
    
    #加入事务
    //$db->beginTransaction();
    
    foreach ($dataList as $key => $val)
    {
        if(!empty($val['openscan']) || !empty($val['fromposition']))
        {
            continue;
        }
        
        #删除对应物料条码记录
        $del_sql    = "DELETE FROM sdb_material_basic_material_conf_special WHERE bm_id=". $val['bm_id'];
        $db->exec($del_sql);
        
        #插入特殊扫码配置
        $insert_sql    = "INSERT INTO sdb_material_basic_material_conf_special SET bm_id=". $val['bm_id'] .", openscan='4', fromposition='1', toposition=1";
        $db->exec($insert_sql);
    }
    
    #提交事务
    //$db->commit();
    
    #release
    unset($dataList, $sql, $del_sql, $insert_sql);
    
    $page++;
    usleep(500000);#暂停0.5秒
    
    #递归调用
    batch_basic_material_conf_special();
}


#脚本执行完成
echo("...Success, Upgrade completed...\n\n");
